﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace DemoApplication.Config
{
    public sealed class Configuration
    {
        private static List<string> s_ClassifyList;
        private static List<string> s_CostUnitList;
        private static List<string> s_QuantityUnitList;

        public static List<string> GetClassifyList()
        {
            if (null == s_ClassifyList)
            {
                s_ClassifyList = GetConfigList("Classify.xml");
            }
            return s_ClassifyList;
        }

        public static List<string> GetCostUnitList()
        {
            if (null == s_CostUnitList)
            {
                s_CostUnitList = GetConfigList("CostUnit.xml");
            }
            return s_CostUnitList;
        }

        public static List<string> GetQuantityUnitList()
        {
            if (null == s_QuantityUnitList)
            {
                s_QuantityUnitList = GetConfigList("QuantityUnit.xml");
            }
            return s_QuantityUnitList;
        }

        private static List<string> GetConfigList(string filename)
        {
            string currentDir = Environment.CurrentDirectory;
            string filepath = Path.Combine(currentDir, "Config", filename);
            List<string> result = new List<string>();

            XElement rootElement = XElement.Load(filepath);
            foreach (XElement childElement in rootElement.Elements())
            {
                result.Add(childElement.Value);
            }
            return result;
        }
    }
}
